home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / os2 / bind493a.zip / res / res_init.c < prev    next >
C/C++ Source or Header  |  1995-06-29  |  19KB  |  649 lines

  1. /*
  2.  * ++Copyright++ 1985, 1989, 1993
  3.  * -
  4.  * Copyright (c) 1985, 1989, 1993
  5.  *    The Regents of the University of California.  All rights reserved.
  6.  * 
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in the
  14.  *    documentation and/or other materials provided with the distribution.
  15.  * 3. All advertising materials mentioning features or use of this software
  16.  *    must display the following acknowledgement:
  17.  *     This product includes software developed by the University of
  18.  *     California, Berkeley and its contributors.
  19.  * 4. Neither the name of the University nor the names of its contributors
  20.  *    may be used to endorse or promote products derived from this software
  21.  *    without specific prior written permission.
  22.  * 
  23.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33.  * SUCH DAMAGE.
  34.  * -
  35.  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  36.  * 
  37.  * Permission to use, copy, modify, and distribute this software for any
  38.  * purpose with or without fee is hereby granted, provided that the above
  39.  * copyright notice and this permission notice appear in all copies, and that
  40.  * the name of Digital Equipment Corporation not be used in advertising or
  41.  * publicity pertaining to distribution of the document or software without
  42.  * specific, written prior permission.
  43.  * 
  44.  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  45.  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  46.  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
  47.  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  48.  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  49.  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  50.  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  51.  * SOFTWARE.
  52.  * -
  53.  * --Copyright--
  54.  */
  55.  
  56. #if defined(LIBC_SCCS) && !defined(lint)
  57. static char sccsid[] = "@(#)res_init.c    8.1 (Berkeley) 6/7/93";
  58. static char rcsid[] = "$Id: res_init.c,v 8.3 1995/06/29 09:26:28 vixie Exp $";
  59. #endif /* LIBC_SCCS and not lint */
  60.  
  61. #include <sys/param.h>
  62. #include <sys/socket.h>
  63. #include <sys/time.h>
  64. #include <netinet/in.h>
  65. #include <arpa/inet.h>
  66. #include <arpa/nameser.h>
  67.  
  68. #include <stdio.h>
  69. #include <ctype.h>
  70. #include <resolv.h>
  71. #if defined(BSD) && (BSD >= 199103)
  72. # include <unistd.h>
  73. # include <stdlib.h>
  74. # include <string.h>
  75. #else
  76. # include "../conf/portability.h"
  77. #endif
  78.  
  79. /*-------------------------------------- info about "sortlist" --------------
  80.  * Marc Majka        1994/04/16
  81.  * Allan Nathanson    1994/10/29 (BIND 4.9.3.x)
  82.  *
  83.  * NetInfo resolver configuration directory support.
  84.  *
  85.  * Allow a NetInfo directory to be created in the hierarchy which
  86.  * contains the same information as the resolver configuration file.
  87.  *
  88.  * - The local domain name is stored as the value of the "domain" property.
  89.  * - The Internet address(es) of the name server(s) are stored as values
  90.  *   of the "nameserver" property.
  91.  * - The name server addresses are stored as values of the "nameserver"
  92.  *   property.
  93.  * - The search list for host-name lookup is stored as values of the
  94.  *   "search" property.
  95.  * - The sortlist comprised of IP address netmask pairs are stored as
  96.  *   values of the "sortlist" property. The IP address and optional netmask
  97.  *   should be seperated by a slash (/) or ampersand (&) character.
  98.  * - Internal resolver variables can be set from the value of the "options"
  99.  *   property.
  100.  */
  101. #if defined(NeXT)
  102. #  include <netinfo/ni.h>
  103. #  define NI_PATH_RESCONF "/locations/resolver"
  104. #  define NI_TIMEOUT 10
  105. static int netinfo_res_init __P((int *haveenv, int *havesearch));
  106. #endif
  107.  
  108. #if defined(USE_OPTIONS_H)
  109. # include "../conf/options.h"
  110. #endif
  111.  
  112. static void res_setoptions __P((char *, char *));
  113.  
  114. #ifdef RESOLVSORT
  115. static const char sort_mask[] = "/&";
  116. #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
  117. static u_int32_t net_mask __P((struct in_addr));
  118. #endif
  119.  
  120. #if !defined(isascii)    /* XXX - could be a function */
  121. # define isascii(c) (!(c & 0200))
  122. #endif
  123.  
  124. /*
  125.  * Resolver state default settings.
  126.  */
  127.  
  128. struct __res_state _res;
  129.  
  130. /*
  131.  * Set up default settings.  If the configuration file exist, the values
  132.  * there will have precedence.  Otherwise, the server address is set to
  133.  * INADDR_ANY and the default domain name comes from the gethostname().
  134.  *
  135.  * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
  136.  * rather than INADDR_ANY ("0.0.0.0") as the default name server address
  137.  * since it was noted that INADDR_ANY actually meant ``the first interface
  138.  * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
  139.  * it had to be "up" in order for you to reach your own name server.  It
  140.  * was later decided that since the recommended practice is to always 
  141.  * install local static routes through 127.0.0.1 for all your network
  142.  * interfaces, that we could solve this problem without a code change.
  143.  *
  144.  * The configuration file should always be used, since it is the only way
  145.  * to specify a default domain.  If you are running a server on your local
  146.  * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
  147.  * in the configuration file.
  148.  *
  149.  * Return 0 if completes successfully, -1 on error
  150.  */
  151. int
  152. res_init()
  153. {
  154.     register FILE *fp;
  155.     register char *cp, **pp;
  156.     register int n;
  157.     char buf[BUFSIZ];
  158.     int nserv = 0;    /* number of nameserver records read from file */
  159.     int haveenv = 0;
  160.     int havesearch = 0;
  161. #ifdef RESOLVSORT
  162.     int nsort = 0;
  163.     char *net;
  164. #endif
  165. #ifndef RFC1535
  166.     int dots;
  167. #endif
  168.  
  169.     /*
  170.      * These three fields used to be statically initialized.  This made
  171.      * it hard to use this code in a shared library.  It is necessary,
  172.      * now that we're doing dynamic initialization here, that we preserve
  173.      * the old semantics: if an application modifies one of these three
  174.      * fields of _res before res_init() is called, res_init() will not
  175.      * alter them.  Of course, if an application is setting them to
  176.      * _zero_ before calling res_init(), hoping to override what used
  177.      * to be the static default, we can't detect it and unexpected results
  178.      * will follow.  Zero for any of these fields would make no sense,
  179.      * so one can safely assume that the applications were already getting
  180.      * unexpected results.
  181.      *
  182.      * _res.options is tricky since some apps were known to diddle the bits
  183.      * before res_init() was first called. We can't replicate that semantic
  184.      * with dynamic initialization (they may have turned bits off that are
  185.      * set in RES_DEFAULT).  Our solution is to declare such applications
  186.      * "broken".  They could fool us by setting RES_INIT but none do (yet).
  187.      */
  188.     if (!_res.retrans)
  189.         _res.retrans = RES_TIMEOUT;
  190.     if (!_res.retry)
  191.         _res.retry = 4;
  192.     if (!(_res.options & RES_INIT))
  193.         _res.options = RES_DEFAULT;
  194.  
  195.     /*
  196.      * This one used to initialize implicitly to zero, so unless the app
  197.      * has set it to something in particular, we can randomize it now.
  198.      */
  199.     if (!_res.id)
  200.         _res.id = res_randomid();
  201.  
  202. #ifdef USELOOPBACK
  203.     _res.nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
  204. #else
  205.     _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
  206. #endif
  207.     _res.nsaddr.sin_family = AF_INET;
  208.     _res.nsa